

from docutils import nodes
from sphinx import addnodes
from sphinx.util.compat import Directive

class package(nodes.Element): pass

class Packages(Directive):
    """
    Directive to mark description of a new package.
    """

    has_content = True
    required_arguments = 1
    optional_arguments = 1
    final_argument_whitespace = False
    option_spec = {}
    
    def run(self):
        
        modname = self.arguments[0].strip()
        
        targetnode = nodes.target('A', '', ids=['package-' + modname], ismod=True)
        self.state.document.note_explicit_target(targetnode)
        
        pck = package()
        
        #print 'Pck', self.content
        
        try :
            self.state.nested_parse(self.content, self.content_offset,
                                pck, match_titles=1)
        except :
            import traceback
            print '!'*10
            traceback.print_exc()
            raise
            
        print '&'*10
        
        ret = [targetnode] + pck.children
        
        indextext = ('%s (package)') % modname
        inode = addnodes.index(entries=[('single', indextext,
                                         'package-' + modname, modname)])
        ret.append(inode)
        
        return ret
        
        
def setup(app):
    
    app.add_node(package)
    app.add_directive('package', Packages)
    